<?php

namespace app\admin\controller\subject;

use app\common\controller\Backend;

class info extends Backend
{
    // 设置关联查询
    protected $relationSearch = true;

    // 当前模型
    protected $model = null;

    // 无需登陆
    protected $noNeedLogin = [];
    // 无需鉴权
    protected $noNeedRight = [];

    public function __construct(){
        parent::__construct();
        $this->model = model('Subject.Subject');
        $this->SubjectModel = model('Subject.Subject');
        $this->OrderModel = model('Subject.Order');
        $this->CommentModel = model('Subject.Comment');
    }

    public function index($ids=NULL){
        $row = $this->model->find($ids);
        if(!$row){
            $this->error('课程不存在');
            exit;
        }
        return $this->view->fetch();
    }

    // 课程订单
    public function order($ids=NULL){
        // 修改一下当前模型
        $this->model = $this->OrderModel;

        // 先判断课程是否存在
        $subject = $this->SubjectModel->find($ids);

        if(!$subject){
            $this->error(__('No Results were found'));
            exit;
        }

        //将请求当中所有的参数去除html标签，去掉两边空白
        $this->request->filter(['strip_tags', 'trim']);

        if($this->request->isAjax()){
            //获取表格所提交过来筛选和排序的一些参数
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            // var_dump(collection($where)->toArray());
            // exit;
            // 表格需要两个返回值参数 总条数和分页的数据
            $total = $this->model
                ->with(['business'])
                ->where($where)
                ->where(['subid'=>$ids])
                ->order($sort,$order)
                ->count(); //返回查询的总数

            $list = $this->model
                ->with(['business'])
                ->where($where)
                ->where(['subid'=>$ids])
                ->order($sort,$order)
                ->limit($offset,$limit)
                ->select();
            // var_dump(collection($list)->toArray());
            // exit;

            // 打印sql语句
            // echo $this->model->getLastSql();
            // exit;

            // 组装数据
            $result = [
                'total' => $total,
                'rows' => $list
            ];
            // 返回给ajax的数据
            return json($result);  
        }
    }


    // 课程评论
    public function comment($ids=NULL){
        // 修改一下当前模型
        $this->model = $this->CommentModel;

        // 先判断课程是否存在
        $subject = $this->SubjectModel->find($ids);

        if(!$subject){
            $this->error(__('No Results were found'));
            exit;
        }

        //将请求当中所有的参数去除html标签，去掉两边空白
        $this->request->filter(['strip_tags', 'trim']);

        if($this->request->isAjax()){
            //获取表格所提交过来筛选和排序的一些参数
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            // var_dump(collection($where)->toArray());
            // exit;
            // 表格需要两个返回值参数 总条数和分页的数据
            $total = $this->model
                ->with(['business'])
                ->where($where)
                ->where(['subid'=>$ids])
                ->order($sort,$order)
                ->count(); //返回查询的总数

            $list = $this->model
                ->with(['business'])
                ->where($where)
                ->where(['subid'=>$ids])
                ->order($sort,$order)
                ->limit($offset,$limit)
                ->select();
            // var_dump(collection($list)->toArray());
            // exit;

            // 打印sql语句
            // echo $this->model->getLastSql();
            // exit;

            // 组装数据
            $result = [
                'total' => $total,
                'rows' => $list
            ];
            // 返回给ajax的数据
            return json($result);  
        }
    }

}
